package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CommoditySetGroupService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.commoditysetgroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CommoditySetGroupDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CommoditySetGroupMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 组套商品管理明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommoditySetGroupServiceImpl implements CommoditySetGroupService {

    @Resource
    private CommoditySetGroupMapper commoditySetGroupMapper;

    @Override
    public Long createCommoditySetGroup(CommoditySetGroupSaveReqVO createReqVO) {
        // 插入
        CommoditySetGroupDO commoditySetGroup = BeanUtils.toBean(createReqVO, CommoditySetGroupDO.class);
        commoditySetGroupMapper.insert(commoditySetGroup);
        // 返回
        return commoditySetGroup.getId();
    }

    @Override
    public void updateCommoditySetGroup(CommoditySetGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateCommoditySetGroupExists(updateReqVO.getId());
        // 更新
        CommoditySetGroupDO updateObj = BeanUtils.toBean(updateReqVO, CommoditySetGroupDO.class);
        commoditySetGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommoditySetGroup(Long id) {
        // 校验存在
        validateCommoditySetGroupExists(id);
        // 删除
        commoditySetGroupMapper.deleteById(id);
    }

    private void validateCommoditySetGroupExists(Long id) {
        if (commoditySetGroupMapper.selectById(id) == null) {
            throw exception(COMMODITY_SET_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public CommoditySetGroupDO getCommoditySetGroup(Long id) {
        return commoditySetGroupMapper.selectById(id);
    }

    @Override
    public PageResult<CommoditySetGroupDO> getCommoditySetGroupPage(CommoditySetGroupPageReqVO pageReqVO) {
        return commoditySetGroupMapper.selectPage(pageReqVO);
    }

}